{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "#### 1.3.1 数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train data shape (150000, 31)\n",
      "test data shape (50000, 30)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "path = './data/'\n",
    "### 1)载入训练集和测试集\n",
    "train_data = pd.read_csv(path+'used_car_train_20200313.csv',sep=' ')\n",
    "test_data = pd.read_csv(path+'used_car_testB_20200421.csv',sep=' ')\n",
    "print('train data shape',train_data.shape)\n",
    "print('test data shape',test_data.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "   SaleID    name   regDate  model  brand  bodyType  fuelType  gearbox  power  \\\n0       0     736  20040402   30.0      6       1.0       0.0      0.0     60   \n1       1    2262  20030301   40.0      1       2.0       0.0      0.0      0   \n2       2   14874  20040403  115.0     15       1.0       0.0      0.0    163   \n3       3   71865  19960908  109.0     10       0.0       0.0      1.0    193   \n4       4  111080  20120103  110.0      5       1.0       0.0      0.0     68   \n\n   kilometer  ...       v_5       v_6       v_7       v_8       v_9      v_10  \\\n0       12.5  ...  0.235676  0.101988  0.129549  0.022816  0.097462 -2.881803   \n1       15.0  ...  0.264777  0.121004  0.135731  0.026597  0.020582 -4.900482   \n2       12.5  ...  0.251410  0.114912  0.165147  0.062173  0.027075 -4.846749   \n3       15.0  ...  0.274293  0.110300  0.121964  0.033395  0.000000 -4.509599   \n4        5.0  ...  0.228036  0.073205  0.091880  0.078819  0.121534 -1.896240   \n\n       v_11      v_12      v_13      v_14  \n0  2.804097 -2.420821  0.795292  0.914762  \n1  2.096338 -1.030483 -1.722674  0.245522  \n2  1.803559  1.565330 -0.832687 -0.229963  \n3  1.285940 -0.501868 -2.438353 -0.478699  \n4  0.910783  0.931110  2.834518  1.923482  \n\n[5 rows x 31 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>SaleID</th>\n      <th>name</th>\n      <th>regDate</th>\n      <th>model</th>\n      <th>brand</th>\n      <th>bodyType</th>\n      <th>fuelType</th>\n      <th>gearbox</th>\n      <th>power</th>\n      <th>kilometer</th>\n      <th>...</th>\n      <th>v_5</th>\n      <th>v_6</th>\n      <th>v_7</th>\n      <th>v_8</th>\n      <th>v_9</th>\n      <th>v_10</th>\n      <th>v_11</th>\n      <th>v_12</th>\n      <th>v_13</th>\n      <th>v_14</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>736</td>\n      <td>20040402</td>\n      <td>30.0</td>\n      <td>6</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>60</td>\n      <td>12.5</td>\n      <td>...</td>\n      <td>0.235676</td>\n      <td>0.101988</td>\n      <td>0.129549</td>\n      <td>0.022816</td>\n      <td>0.097462</td>\n      <td>-2.881803</td>\n      <td>2.804097</td>\n      <td>-2.420821</td>\n      <td>0.795292</td>\n      <td>0.914762</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>2262</td>\n      <td>20030301</td>\n      <td>40.0</td>\n      <td>1</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>15.0</td>\n      <td>...</td>\n      <td>0.264777</td>\n      <td>0.121004</td>\n      <td>0.135731</td>\n      <td>0.026597</td>\n      <td>0.020582</td>\n      <td>-4.900482</td>\n      <td>2.096338</td>\n      <td>-1.030483</td>\n      <td>-1.722674</td>\n      <td>0.245522</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>14874</td>\n      <td>20040403</td>\n      <td>115.0</td>\n      <td>15</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>163</td>\n      <td>12.5</td>\n      <td>...</td>\n      <td>0.251410</td>\n      <td>0.114912</td>\n      <td>0.165147</td>\n      <td>0.062173</td>\n      <td>0.027075</td>\n      <td>-4.846749</td>\n      <td>1.803559</td>\n      <td>1.565330</td>\n      <td>-0.832687</td>\n      <td>-0.229963</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>71865</td>\n      <td>19960908</td>\n      <td>109.0</td>\n      <td>10</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>193</td>\n      <td>15.0</td>\n      <td>...</td>\n      <td>0.274293</td>\n      <td>0.110300</td>\n      <td>0.121964</td>\n      <td>0.033395</td>\n      <td>0.000000</td>\n      <td>-4.509599</td>\n      <td>1.285940</td>\n      <td>-0.501868</td>\n      <td>-2.438353</td>\n      <td>-0.478699</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>111080</td>\n      <td>20120103</td>\n      <td>110.0</td>\n      <td>5</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>68</td>\n      <td>5.0</td>\n      <td>...</td>\n      <td>0.228036</td>\n      <td>0.073205</td>\n      <td>0.091880</td>\n      <td>0.078819</td>\n      <td>0.121534</td>\n      <td>-1.896240</td>\n      <td>0.910783</td>\n      <td>0.931110</td>\n      <td>2.834518</td>\n      <td>1.923482</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 31 columns</p>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 1.3.2 分类指标评价计算示例"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ACC: 0.75\n",
      "Precision: 1.0\n",
      "Recall: 0.5\n",
      "F1-score: 0.6666666666666666\n"
     ]
    }
   ],
   "source": [
    "## accuracy\n",
    "from sklearn import metrics\n",
    "\n",
    "y_pred=[0,1,0,0]\n",
    "y_true=[0,1,0,1]\n",
    "print('ACC:',metrics.accuracy_score(y_true,y_pred))\n",
    "print('Precision:',metrics.precision_score(y_true,y_pred))\n",
    "print('Recall:',metrics.recall_score(y_true,y_pred))\n",
    "print('F1-score:',metrics.f1_score(y_true,y_pred))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 1.3.3 回归指标评价计算示例"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MSE: 0.2871428571428571\n",
      "RMSE: 0.5358571238146014\n",
      "MAE 0.4142857142857143\n",
      "MAPE 0.1461904761904762\n",
      "R2-score: 0.9486081370449679\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "import numpy as np\n",
    "\n",
    "def mape(y_true, y_pred):\n",
    "    return np.mean(np.abs((y_pred-y_true) / y_true))\n",
    "\n",
    "y_true = np.array([1.0, 5.0, 4.0, 3.0, 2.0, 5.0, -3.0])\n",
    "y_pred = np.array([1.0, 4.5, 3.8, 3.2, 3.0, 4.8, -2.2])\n",
    "\n",
    "print('MSE:', metrics.mean_squared_error(y_true,y_pred))\n",
    "print('RMSE:', np.sqrt(metrics.mean_squared_error(y_true,y_pred)))\n",
    "print('MAE', metrics.mean_absolute_error(y_true,y_pred))\n",
    "print('MAPE', mape(y_true,y_pred))\n",
    "\n",
    "y_true = [3, -0.5, 2, 7]\n",
    "y_pred = [2.5, 0.0, 2, 8]\n",
    "print('R2-score:',metrics.r2_score(y_true,y_pred))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}